package com.atguigu.controller;

import com.atguigu.pojo.User;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("validated")
public class ValidatedController {
    @PostMapping("user")
    public Object saveUser(@Validated @RequestBody User user, BindingResult bindingResult){
       if(bindingResult.hasErrors()){
           System.out.println("error");
           List<FieldError> fieldErrors = bindingResult.getFieldErrors();
           for (FieldError fieldError : fieldErrors) {
               String defaultMessage = fieldError.getDefaultMessage();
               System.out.println(defaultMessage);
               return "cahshuyichang";
           }

       }
        System.out.println("success");
       return user;
    }

}
